#include "partie.h"
#include <iostream>

using namespace std;

Partie::Partie(QString nomsjoueurs[6], int nombre)
{
    m_nbjoueurs = nombre;

    /*
    if(m_nbjoueurs < 4)
        m_mode = MODEQUINZE;
    else
        m_mode = MODEDIX;
    */

    //Gestion du mode a ajouter plus tard

    //Remplissage de la MultiMap représentant le plateau de jeu
    int position = 120;

    //4 premiers cas de la partie superieure du remplissage
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < i+1; j++)
        {
            m_plateau.insert(i , new Emplacements(position));
            position--;
        }
    }
    //4  dernier cas de la partie superieur du remplissage
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 13 - i ; j++)
        {
            m_plateau.insert(i + 4, new Emplacements(position));
            position--;
        }
    }

    //cas du remplissage de la ligne centrale
    for(int i = 0; i < 9; i++)
    {
        m_plateau.insert(8, new Emplacements(position));
        position--;
    }

    //4  dernier cas de la partie superieur du remplissage
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 13 - ((4 - 1) - i); j++)
        {
            m_plateau.insert(i + 9, new Emplacements(position));
            position--;
        }
    }
    //4  dernier cas de la partie superieur du remplissage
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4 - i; j++)
        {
            m_plateau.insert(i + 13, new Emplacements(position));
            position--;
        }
    }


    //mise en place des sets de zones (modifier selon le mode, peut etre creer une fonction a part)
    m_setdix[SUD] = new QSet<int>();

    m_setdix[SUD]->insert(0);
    m_setdix[SUD]->insert(1);
    m_setdix[SUD]->insert(2);
    m_setdix[SUD]->insert(3);
    m_setdix[SUD]->insert(4);
    m_setdix[SUD]->insert(5);
    m_setdix[SUD]->insert(6);
    m_setdix[SUD]->insert(7);
    m_setdix[SUD]->insert(8);
    m_setdix[SUD]->insert(9);

    m_setdix[SUDOUEST] = new QSet<int>();

    m_setdix[SUDOUEST]->insert(10);
    m_setdix[SUDOUEST]->insert(11);
    m_setdix[SUDOUEST]->insert(12);
    m_setdix[SUDOUEST]->insert(13);
    m_setdix[SUDOUEST]->insert(23);
    m_setdix[SUDOUEST]->insert(24);
    m_setdix[SUDOUEST]->insert(25);
    m_setdix[SUDOUEST]->insert(35);
    m_setdix[SUDOUEST]->insert(36);
    m_setdix[SUDOUEST]->insert(46);

    m_setdix[NORDOUEST] = new QSet<int>();

    m_setdix[NORDOUEST]->insert(98);
    m_setdix[NORDOUEST]->insert(99);
    m_setdix[NORDOUEST]->insert(100);
    m_setdix[NORDOUEST]->insert(101);
    m_setdix[NORDOUEST]->insert(86);
    m_setdix[NORDOUEST]->insert(87);
    m_setdix[NORDOUEST]->insert(88);
    m_setdix[NORDOUEST]->insert(75);
    m_setdix[NORDOUEST]->insert(76);
    m_setdix[NORDOUEST]->insert(65);

    m_setdix[NORD] = new QSet<int>();

    m_setdix[NORD]->insert(120);
    m_setdix[NORD]->insert(119);
    m_setdix[NORD]->insert(118);
    m_setdix[NORD]->insert(117);
    m_setdix[NORD]->insert(116);
    m_setdix[NORD]->insert(115);
    m_setdix[NORD]->insert(114);
    m_setdix[NORD]->insert(113);
    m_setdix[NORD]->insert(112);
    m_setdix[NORD]->insert(111);

    m_setdix[NORDEST] = new QSet<int>();

    m_setdix[NORDEST]->insert(74);
    m_setdix[NORDEST]->insert(84);
    m_setdix[NORDEST]->insert(85);
    m_setdix[NORDEST]->insert(95);
    m_setdix[NORDEST]->insert(96);
    m_setdix[NORDEST]->insert(97);
    m_setdix[NORDEST]->insert(107);
    m_setdix[NORDEST]->insert(108);
    m_setdix[NORDEST]->insert(109);
    m_setdix[NORDEST]->insert(110);

    m_setdix[SUDEST] = new QSet<int>();

    m_setdix[SUDEST]->insert(19);
    m_setdix[SUDEST]->insert(20);
    m_setdix[SUDEST]->insert(21);
    m_setdix[SUDEST]->insert(22);
    m_setdix[SUDEST]->insert(32);
    m_setdix[SUDEST]->insert(33);
    m_setdix[SUDEST]->insert(34);
    m_setdix[SUDEST]->insert(44);
    m_setdix[SUDEST]->insert(45);
    m_setdix[SUDEST]->insert(55);
    /////////////////////////////

    //Remplissage de la liste des joueurs
    //A rectifier avec le nombre de joueur
    for(int i = 0; i < 6; i++)
    {
        m_joueurs[i] = new Joueur(nomsjoueurs[i], i, i, i);
    }

}

Partie::~Partie()
{
}

Emplacements * Partie::intToEmplacement(int numero, int key)
{
    Emplacements * e;
    foreach( e ,m_plateau.values(key))  //Parcours de la ligne key
        if(e->m_numero == numero)       //Teste si l'emplacement en cours a le bon numero
            return e;
    return NULL;
}

bool Partie::isDeplacementValide(Emplacements *src, Emplacements *dest)
{

   //Penser a ajouter verification dans la version jouable : src->m_numjoueur == joueur en cours

    if(!src->isPiece()) return false; //emplacement source n'est pas une piece, erreur

    int voisin = src->isVoisin(dest);   //recuperation du numero de voisin associé a dest dans le tableau des voisins de sources
                                        //si dest n'est pas un voisin de src, voisin = -1;

    if(voisin != -1) //Cas deplacement simple
    {
        if(!dest->isPiece()) return true;                           //emplacement libre

        //else if(!dest->m_voisins[voisin]->isPiece()) return true; //cas particulier, deplacement possible a dest->m_voisins[voisin], impossible a traiter ici.
                                                                    //peut etre créer une fonction a part, ou oublier ce traitement

        return false;                                               //deux pieces l'une derriere l'autre, deplacement impossible
    }
    else //Cas deplacement plus complexe (saut)
    {
        if(!dest->isPiece())                                                //on verifie si la destination n'est pas une piece
            for(int i = 0; i < 6; i++)                                      //
            {                                                               //On parcours tous les voisins de source
                if(src->m_voisins[i]== NULL) continue;                      //
                if(src->m_voisins[i]->isPiece())                            //On verifie que le voisin de source est une piece
                    if(src->m_voisins[i]->isVoisin(dest) == i) return true; //On verifie si l'emplacement dest est le voisin de la piece voisine de src,
                                                                            //et si son numero de voisin (i) est le même que celui du voisin traité precedement,
                                                                            //deplacement possible dans la direction i (Sud, Sud ouest, nord ouest, etc..)

            }
        return false; //emplacement dest paumé n'importe ou, hors de porté, deplacement impossible
    }
}

void Partie::deplacerPiece(Emplacements *src, Emplacements *dest)
{
    if(isDeplacementValide(src, dest))
    {
        dest->m_numjoueur = src->m_numjoueur;
        dest->m_couleurp = src->m_couleurp;
        src->m_numjoueur = -1;
        src->m_couleurp = -1;
    }
    else
    {
        //envoyer message d'erreur au joueur en cours
    }
}

void Partie::remplirVoisins()
{
    //ligne 5 à 8
    for(int i = 5; i < 8; ++i)
    {
        for(int j = 0; j < m_plateau.values(i).count(); ++j)
        {

            if(m_plateau.values(i).startsWith(m_plateau.values(i).at(j)))
            {
               m_plateau.values(i).at(j)->m_voisins[GAUCHE]    = NULL;
               m_plateau.values(i).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
               m_plateau.values(i).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() -1),i+1);
               m_plateau.values(i).at(j)->m_voisins[BASGAUCHE] = NULL;
            }

            else if(m_plateau.values(i).endsWith(m_plateau.values(i).at(j)))
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[DROITE]    = NULL;
                m_plateau.values(i).at(j)->m_voisins[BASDROITE] = NULL;
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);

            }
            else
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() -1),i+1);
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
            }
            m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
            m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count() +1,i-1);

        }
    }

    //ligne 8
    for(int j = 0; j < m_plateau.values(8).count(); ++j)
    {
        m_plateau.values(8).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(8).at(j)->m_numero + m_plateau.values(8).count(),8-1);
        m_plateau.values(8).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(8).at(j)->m_numero + m_plateau.values(8).count() +1,8-1);
        m_plateau.values(8).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(8).at(j)->m_numero - m_plateau.values(8).count(),8+1);
        m_plateau.values(8).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(8).at(j)->m_numero - (m_plateau.values(8).count() +1),8+1);

        if(m_plateau.values(8).startsWith(m_plateau.values(8).at(j)))
        {
            m_plateau.values(8).at(j)->m_voisins[GAUCHE] = NULL;
            m_plateau.values(8).at(j)->m_voisins[DROITE] = intToEmplacement(m_plateau.values(8).at(j)->m_numero +1,8);
        }

        else if(m_plateau.values(8).endsWith(m_plateau.values(8).at(j)))
        {
            m_plateau.values(8).at(j)->m_voisins[DROITE] = NULL;
            m_plateau.values(8).at(j)->m_voisins[GAUCHE] = intToEmplacement(m_plateau.values(8).at(j)->m_numero -1,8);
        }

        else
        {
            m_plateau.values(8).at(j)->m_voisins[DROITE] = intToEmplacement(m_plateau.values(8).at(j)->m_numero +1,8);
            m_plateau.values(8).at(j)->m_voisins[GAUCHE] = intToEmplacement(m_plateau.values(8).at(j)->m_numero -1,8);
        }
    }

    //ligne 9 à 12
    for(int i = 9; i < 13; ++i)
    {
        for(int j = 0; j < m_plateau.values(i).count(); ++j)
        {
            if(m_plateau.values(i).startsWith(m_plateau.values(i).at(j)))
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]    = NULL;
                m_plateau.values(i).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = NULL;
            }

            else if(m_plateau.values(i).endsWith(m_plateau.values(i).at(j)))
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[DROITE]    = NULL;
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = NULL;
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() -1),i-1);

            }
            else
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() -1),i-1);
            }
            m_plateau.values(i).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() +1),i+1);
            m_plateau.values(i).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
        }
    }

    ////////////////////////////ligne 4
    //4 premiers
    for(int j = 0; j < 4; ++j)
    {

        if(m_plateau.values(4).startsWith(m_plateau.values(4).at(j)))
        {
            m_plateau.values(4).at(j)->m_voisins[GAUCHE]    = NULL;
            m_plateau.values(4).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero +1,4);
            m_plateau.values(4).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - (m_plateau.values(4).count() -1),4+1);
            m_plateau.values(4).at(j)->m_voisins[BASGAUCHE] = NULL;
        }
        else
        {
            m_plateau.values(4).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero -1,4);
            m_plateau.values(4).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero +1,4);
            m_plateau.values(4).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - (m_plateau.values(4).count() -1),4+1);
            m_plateau.values(4).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - m_plateau.values(4).count(),4+1);
        }

        m_plateau.values(4).at(j)->m_voisins[HAUTDROITE] = NULL;
        m_plateau.values(4).at(j)->m_voisins[HAUTGAUCHE] = NULL;
    }

    //4 derniers
    for(int j = 9; j < 13; ++j)
    {

        if(m_plateau.values(4).endsWith(m_plateau.values(4).at(j)))
        {
            m_plateau.values(4).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero -1,4);
            m_plateau.values(4).at(j)->m_voisins[DROITE]     = NULL;
            m_plateau.values(4).at(j)->m_voisins[BASDROITE] = NULL;
            m_plateau.values(4).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - m_plateau.values(4).count(),4+1);
        }
        else
        {
            m_plateau.values(4).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero -1,4);
            m_plateau.values(4).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(4).at(j)->m_numero +1,4);
            m_plateau.values(4).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - (m_plateau.values(4).count() -1),4+1);
            m_plateau.values(4).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - m_plateau.values(4).count(),4+1);
        }

        m_plateau.values(4).at(j)->m_voisins[HAUTDROITE] = NULL;
        m_plateau.values(4).at(j)->m_voisins[HAUTGAUCHE] = NULL;
    }

    //5 emplacements du milieu
    for(int j = 4; j < 9; ++j)
    {
        m_plateau.values(4).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero -1,4);
        m_plateau.values(4).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(4).at(j)->m_numero +1,4);
        m_plateau.values(4).at(j)->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - (m_plateau.values(4).count() -1),4+1);
        m_plateau.values(4).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero - m_plateau.values(4).count(),4+1);

        if(j == 4)
        {
            m_plateau.values(4).at(j)->m_voisins[HAUTGAUCHE] = NULL;
            m_plateau.values(4).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero + 9,4-1);
        }
        else if(j == 8)
        {
            m_plateau.values(4).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero + 8,4-1);
            m_plateau.values(4).at(j)->m_voisins[HAUTDROITE] = NULL;
        }

        else
        {
            m_plateau.values(4).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero + 8,4-1);
            m_plateau.values(4).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(4).at(j)->m_numero + 9,4-1);
        }
    }
    /////////////////////////////////ligne 4

    ////////////////////////////ligne 12
    //4 premiers
    for(int j = 0; j < 4; ++j)
    {

        if(m_plateau.values(12).startsWith(m_plateau.values(12).at(j)))
        {
            m_plateau.values(12).at(j)->m_voisins[GAUCHE]    = NULL;
            m_plateau.values(12).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(12).at(j)->m_numero +1,12);
            m_plateau.values(12).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count(),12-1);
            m_plateau.values(12).at(j)->m_voisins[HAUTGAUCHE] = NULL;
        }
        else
        {
            m_plateau.values(12).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(12).at(j)->m_numero -1,12);
            m_plateau.values(12).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(12).at(j)->m_numero +1,12);
            m_plateau.values(12).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count(),12-1);
            m_plateau.values(12).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count() -1,12-1);
        }

        m_plateau.values(12).at(j)->m_voisins[BASDROITE] = NULL;
        m_plateau.values(12).at(j)->m_voisins[BASGAUCHE] = NULL;
    }

    //4 derniers
    for(int j = 9; j < 13; ++j)
    {
        if(m_plateau.values(12).endsWith(m_plateau.values(12).at(j)))
        {
            m_plateau.values(12).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(12).at(j)->m_numero -1,12);
            m_plateau.values(12).at(j)->m_voisins[DROITE]     = NULL;
            m_plateau.values(12).at(j)->m_voisins[HAUTDROITE] = NULL;
            m_plateau.values(12).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count() -1,12-1);
        }
        else
        {
            m_plateau.values(12).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(12).at(j)->m_numero -1,12);
            m_plateau.values(12).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(12).at(j)->m_numero +1,12);
            m_plateau.values(12).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count(),12-1);
            m_plateau.values(12).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count() -1,12-1);
        }

        m_plateau.values(12).at(j)->m_voisins[BASDROITE] = NULL;
        m_plateau.values(12).at(j)->m_voisins[BASGAUCHE] = NULL;
    }

    //5 emplacements du milieu
    for(int j = 4; j < 9; ++j)
    {
        m_plateau.values(12).at(j)->m_voisins[GAUCHE]    = intToEmplacement(m_plateau.values(12).at(j)->m_numero -1,12);
        m_plateau.values(12).at(j)->m_voisins[DROITE]    = intToEmplacement(m_plateau.values(12).at(j)->m_numero +1,12);
        m_plateau.values(12).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count(),12-1);
        m_plateau.values(12).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero + m_plateau.values(12).count() -1,12-1);

        if(j == 4)
        {
            m_plateau.values(12).at(j)->m_voisins[BASGAUCHE] = NULL;
            m_plateau.values(12).at(j)->m_voisins[BASDROITE] =  intToEmplacement(m_plateau.values(12).at(j)->m_numero -8,12+1);
        }
        else if(j == 8)
        {
            m_plateau.values(12).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero -9,12+1);
            m_plateau.values(12).at(j)->m_voisins[BASDROITE] =  NULL;
        }

        else
        {
            m_plateau.values(12).at(j)->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(12).at(j)->m_numero -9,12+1);
            m_plateau.values(12).at(j)->m_voisins[BASDROITE] =  intToEmplacement(m_plateau.values(12).at(j)->m_numero -8,12+1);
        }
    }
    /////////////////////////////////ligne 12

    //pointe nord
    //cas du premier elements
    m_plateau.values(0).first()->m_voisins[HAUTDROITE] = NULL;
    m_plateau.values(0).first()->m_voisins[HAUTGAUCHE] = NULL;
    m_plateau.values(0).first()->m_voisins[DROITE] = NULL;
    m_plateau.values(0).first()->m_voisins[GAUCHE] = NULL;
    m_plateau.values(0).first()->m_voisins[BASDROITE] = intToEmplacement(m_plateau.values(0).first()->m_numero - (m_plateau.values(0).count()),0+1);
    m_plateau.values(0).first()->m_voisins[BASGAUCHE] = intToEmplacement(m_plateau.values(0).first()->m_numero - (m_plateau.values(0).count() +1),0+1);

    for(int i = 1; i < 3; ++i)
    {
        for(int j = 0; j < m_plateau.values(i).count(); ++j)
        {
            if(m_plateau.values(i).startsWith(m_plateau.values(i).at(j)))
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]     = NULL;
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = NULL;
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                m_plateau.values(i).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() +1),i+1);
            }
            else if(m_plateau.values(i).endsWith(m_plateau.values(i).at(j)))
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() -1),i-1);
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = NULL;
                m_plateau.values(i).at(j)->m_voisins[DROITE]     = NULL;
                m_plateau.values(i).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() +1),i+1);
            }
            else
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() -1),i-1);
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                m_plateau.values(i).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() +1),i+1);
            }
        }
    }

    //derniere ligne de pointe nord
    for(int j = 0; j < m_plateau.values(3).count(); ++j)
    {

        if(m_plateau.values(3).startsWith(m_plateau.values(3).at(j)))
        {
            m_plateau.values(3).at(j)->m_voisins[GAUCHE]     = NULL;
            m_plateau.values(3).at(j)->m_voisins[HAUTGAUCHE] = NULL;
            m_plateau.values(3).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(3).at(j)->m_numero + m_plateau.values(3).count(),3-1);
            m_plateau.values(3).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(3).at(j)->m_numero +1,3);
            m_plateau.values(3).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(3).at(j)->m_numero - 8,3+1);
            m_plateau.values(3).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(3).at(j)->m_numero - 9,3+1);
        }

        else if(m_plateau.values(3).endsWith(m_plateau.values(3).at(j)))
        {
            m_plateau.values(3).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(3).at(j)->m_numero -1,3);
            m_plateau.values(3).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(3).at(j)->m_numero + (m_plateau.values(3).count() -1),3-1);
            m_plateau.values(3).at(j)->m_voisins[HAUTDROITE] = NULL;
            m_plateau.values(3).at(j)->m_voisins[DROITE]     = NULL;
            m_plateau.values(3).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(3).at(j)->m_numero - 8,3+1);
            m_plateau.values(3).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(3).at(j)->m_numero - 9,3+1);
        }
        else
        {
            m_plateau.values(3).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(3).at(j)->m_numero -1,3);
            m_plateau.values(3).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(3).at(j)->m_numero + (m_plateau.values(3).count() -1),3-1);
            m_plateau.values(3).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(3).at(j)->m_numero + m_plateau.values(3).count(),3-1);
            m_plateau.values(3).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(3).at(j)->m_numero +1,3);
            m_plateau.values(3).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(3).at(j)->m_numero - 8,3+1);
            m_plateau.values(3).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(3).at(j)->m_numero - 9,3+1);
        }
    }

    //pointe sud
    //cas du premier elements
    m_plateau.values(16).first()->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(16).first()->m_numero + (m_plateau.values(16).count() +1),16-1);
    m_plateau.values(16).first()->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(16).first()->m_numero + m_plateau.values(16).count(),16-1);
    m_plateau.values(16).first()->m_voisins[DROITE] = NULL;
    m_plateau.values(16).first()->m_voisins[GAUCHE] = NULL;
    m_plateau.values(16).first()->m_voisins[BASDROITE] = NULL;
    m_plateau.values(16).first()->m_voisins[BASGAUCHE] = NULL;

    for(int i = 14; i < 16; ++i)
    {
        for(int j = 0; j < m_plateau.values(i).count(); ++j)
        {

            if(m_plateau.values(i).startsWith(m_plateau.values(i).at(j)))
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]     = NULL;
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() +1),i-1);
                m_plateau.values(i).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() -1),i+1);
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE]  = NULL;
            }

            else if(m_plateau.values(i).endsWith(m_plateau.values(i).at(j)))
            {
                 m_plateau.values(i).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                 m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                 m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() +1),i-1);
                 m_plateau.values(i).at(j)->m_voisins[DROITE]     = NULL;
                 m_plateau.values(i).at(j)->m_voisins[BASDROITE]  = NULL;
                 m_plateau.values(i).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
            }
            else
            {
                m_plateau.values(i).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero -1,i);
                m_plateau.values(i).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + m_plateau.values(i).count(),i-1);
                m_plateau.values(i).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(i).at(j)->m_numero + (m_plateau.values(i).count() +1),i-1);
                m_plateau.values(i).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(i).at(j)->m_numero +1,i);
                m_plateau.values(i).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - (m_plateau.values(i).count() -1),i+1);
                m_plateau.values(i).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(i).at(j)->m_numero - m_plateau.values(i).count(),i+1);
            }
        }
    }

    //premiere ligne de pointe sud
    for(int j = 0; j < m_plateau.values(13).count(); ++j)
    {

        if(m_plateau.values(13).startsWith(m_plateau.values(13).at(j)))
        {
            m_plateau.values(13).at(j)->m_voisins[GAUCHE]     = NULL;
            m_plateau.values(13).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(13).at(j)->m_numero + 8,13-1);
            m_plateau.values(13).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(13).at(j)->m_numero + 9,13-1);
            m_plateau.values(13).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(13).at(j)->m_numero +1,13);
            m_plateau.values(13).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(13).at(j)->m_numero - (m_plateau.values(13).count() -1),13+1);
            m_plateau.values(13).at(j)->m_voisins[BASGAUCHE]  = NULL;
        }

        else if(m_plateau.values(13).endsWith(m_plateau.values(13).at(j)))
        {
            m_plateau.values(13).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(13).at(j)->m_numero -1,13);
            m_plateau.values(13).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(13).at(j)->m_numero + 8,13-1);
            m_plateau.values(13).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(13).at(j)->m_numero + 9,13-1);
            m_plateau.values(13).at(j)->m_voisins[DROITE]     = NULL;
            m_plateau.values(13).at(j)->m_voisins[BASDROITE]  = NULL;
            m_plateau.values(13).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(13).at(j)->m_numero - m_plateau.values(13).count(),13+1);
        }
        else
        {
            m_plateau.values(13).at(j)->m_voisins[GAUCHE]     = intToEmplacement(m_plateau.values(13).at(j)->m_numero -1,13);
            m_plateau.values(13).at(j)->m_voisins[HAUTGAUCHE] = intToEmplacement(m_plateau.values(13).at(j)->m_numero + 8,13-1);
            m_plateau.values(13).at(j)->m_voisins[HAUTDROITE] = intToEmplacement(m_plateau.values(13).at(j)->m_numero + 9,13-1);
            m_plateau.values(13).at(j)->m_voisins[DROITE]     = intToEmplacement(m_plateau.values(13).at(j)->m_numero +1,13);
            m_plateau.values(13).at(j)->m_voisins[BASDROITE]  = intToEmplacement(m_plateau.values(13).at(j)->m_numero - (m_plateau.values(13).count() -1),13+1);
            m_plateau.values(13).at(j)->m_voisins[BASGAUCHE]  = intToEmplacement(m_plateau.values(13).at(j)->m_numero - m_plateau.values(13).count(),13+1);
        }
    }
}
